package com.resou.reader.bookdetail.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.resou.reader.R;
import com.resou.reader.api.entry.BookDetailBean;
import com.resou.reader.api.entry.ChapterItemBean;
import com.resou.reader.base.v.BaseActivity;
import com.resou.reader.bookdetail.iview.ICatalogueView;
import com.resou.reader.bookdetail.adapter.CatalogueAdapter;
import com.resou.reader.bookshelf.listener.OnItemClickListener;
import com.resou.reader.commom.Constant;
import com.resou.reader.mine.IView.UserInstance;
import com.resou.reader.utils.DeviceUtils;
import com.resou.reader.utils.ToastUtil;
import com.resou.reader.view.Loading;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 目录Activity
 */
public class CatalogueActivity extends BaseActivity<CataloguePresenter> implements ICatalogueView, OnItemClickListener {

    @BindView(R.id.catalogue_recycler)
    RecyclerView mRecyclerView;
    @BindView(R.id.order_btn)
    TextView mOrderText;
    @BindView(R.id.catalogue_sum)
    TextView mChapterSum;

    private CatalogueAdapter mAdapter = new CatalogueAdapter(this);
    private BookDetailBean bookDetailBean;
    private int chapterSum = 0;
    private String bookId;
    private boolean isRef = false;

    @Override
    public int getLayoutId() {
        return R.layout.activity_catalogue;
    }

    @Override
    protected void initPresenter() {
        String bookId = getIntent().getStringExtra(Constant.EXTRA_BOOK_ID);
        bookDetailBean = getIntent().getParcelableExtra(Constant.EXTRA_CATALOGUE_BOOK_DETAIL_BEAN);

//        int chapterSum = getIntent().getIntExtra(Constant.EXTRA_CHAPTER_SUM, 0);
//        String bookName = getIntent().getStringExtra(Constant.EXTRA_BOOK_NAME);
//        String shareUrl = getIntent().getStringExtra(Constant.EXTRA_SHARE_URL);
//        String description = getIntent().getStringExtra(Constant.EXTRA_DESCRIPTION);
        chapterSum = bookDetailBean.getChapterNum();
        String bookName = bookDetailBean.getNovelName();
        String shareUrl = bookDetailBean.getShareUrl();
        String description = bookDetailBean.getNovelDescription();
        presenter = new CataloguePresenter(this, bookId, bookDetailBean);
    }

    //catalogue_item_layout
    @Override
    protected void initView() {
        showLoading();
        mChapterSum.setText("共" + String.valueOf(chapterSum) + "章");
        LinearLayoutManager llm = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(llm);
        mAdapter.setType("cata");
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void retryRefreshData() {
        presenter.getCatalogue(bookId, 0, UserInstance.getToken(), DeviceUtils.getIMEI(this), bookDetailBean);
    }

    public static void startActivity(Activity context, String bookId, int chapterSum, String bookName, String shareUrl, String description) {
        Intent intent = new Intent(context, CatalogueActivity.class);
        intent.putExtra(Constant.EXTRA_BOOK_ID, bookId);
        intent.putExtra(Constant.EXTRA_CHAPTER_SUM, chapterSum);
        intent.putExtra(Constant.EXTRA_BOOK_NAME, bookName);
        intent.putExtra(Constant.EXTRA_SHARE_URL, shareUrl);
        intent.putExtra(Constant.EXTRA_DESCRIPTION, description);
        context.startActivity(intent);
        context.overridePendingTransition(R.anim.anim_detail_enter, R.anim.anim_no);
    }

    public static void startActivity(Activity context, String bookId, BookDetailBean bookDetailBean) {
        Intent intent = new Intent(context, CatalogueActivity.class);
        intent.putExtra(Constant.EXTRA_BOOK_ID, bookId);
        intent.putExtra(Constant.EXTRA_CATALOGUE_BOOK_DETAIL_BEAN, bookDetailBean);
        context.startActivity(intent);
        context.overridePendingTransition(R.anim.anim_detail_enter, R.anim.anim_no);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        setResult(RESULT_OK, data);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.anim_no, R.anim.anim_detail_exit);
    }

    @Override
    protected void onStart() {
        super.onStart();
        bookId = getIntent().getStringExtra(Constant.EXTRA_BOOK_ID);
        presenter.getCatalogue(bookId, 0, UserInstance.getToken(), DeviceUtils.getIMEI(this), bookDetailBean);
    }

    @Override
    public void refresh(List<ChapterItemBean> list) {
        dissLoading();
        mAdapter.setList(list);
        if (isRef) {
            ToastUtil.makeShortToast("章节列表已为最新");
            isRef = false;
        }
    }

    @Override
    public void showError(Throwable throwable) {
        ToastUtil.makeShortToast("网络异常");
        mAdapter.clear();
        showNetErrorView(R.id.rlContent);
    }

    @Override
    public Context getViewContext() {
        return this;
    }

    @OnClick({R.id.order_btn, R.id.refresh_btn})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.order_btn:
                mAdapter.reverse();
                if (getString(R.string.order_back).equals(mOrderText.getText())) {
                    mOrderText.setText(getString(R.string.order_normal));
                } else {
                    mOrderText.setText(getString(R.string.order_back));
                }
                break;
            case R.id.refresh_btn:
                presenter.getCatalogue(bookId, 0, UserInstance.getToken(), DeviceUtils.getIMEI(this), bookDetailBean);
                isRef = true;
                break;
        }
    }

    @Override
    public void onItemClicked(RecyclerView.Adapter adapter, View itemView, int index) {
        Log.d("mjm","index:" + index);
        presenter.reader(index);
    }
}
